﻿package igz.fleaxy.ui.form.radio
{
	import flash.display.Sprite;
	import igz.fleaxy.ui.layout.LayoutOrientation;
	import igz.fleaxy.util.ImageUtil;
	import igz.fleaxy.util.ObjectUtil;
	
	/**
	 * @author Intelygenz | www.intelygenz.com
	 */
	public class RadioGroupUi extends Sprite {
		protected var _Settings	: Object;
		
		protected var _RadioButtonsArray : Array;
		
		protected var _RadioMaxHeight : Number;
		protected var _RadioMaxWidth : Number;
		
		protected var _RadioButtonSelectedUi : RadioButtonUi;
		public function get $RadioButtonSelectedUi():RadioButtonUi { 
			return _RadioButtonSelectedUi;
		}

		/**
		 * 
		 * @param	p_radioButtonSettings -> array de settings de RadioButton (ver CheckBoxUi)
		 * @param	p_element_selected
		 * @param	p_settings ->{
								"orientation"	: LayoutOrientation.$HORIZONTAL
								,"width" 		: null
								,"height"		: null
								,"internalGap"	: 5
								// RadioButtons
								,"imgCheck"     : null
								,"imgUncheck"   : null
								,"labelStyle"	: "defaultText"
								,"columns"      : 2
			}
		 */
		public function RadioGroupUi(p_radioButtonSettings:Array , p_settings:Object = null ) 
		{
			_Settings = {
				 "orientation"	: LayoutOrientation.$HORIZONTAL
				,"width" 		: null
				,"height"		: null
				,"internalGap"	: 5
				// RadioButtons
				,"imgCheck"     : null
				,"imgUncheck"   : null
				,"labelStyle"	: "defaultText"
				,"columns"      : 2		//Para el modo tabla
			}
			ObjectUtil.$Merge( p_settings, _Settings );		

			_RadioButtonsArray = new Array();
			_RadioMaxWidth = 0;
			_RadioMaxHeight = 0;
			
			var radioButtonUi:RadioButtonUi;
			var radioSettings : Object;
			
			for (var i:int; i < p_radioButtonSettings.length; i++ ) {
				radioSettings = p_radioButtonSettings[i];
				
				if ( _Settings["imgCheck"] != null  &&  _Settings["imgUncheck"] != null) {
					radioSettings["imgCheck"]   = ImageUtil.$GetBitmap(_Settings["imgCheck"]);
					radioSettings["imgUncheck"] = ImageUtil.$GetBitmap(_Settings["imgUncheck"]);
				}
				if ( !radioSettings.hasOwnProperty("labelStyle") ) {
					radioSettings["labelStyle"] = _Settings["labelStyle"];
				}

				radioButtonUi = new RadioButtonUi( this, radioSettings );
				
				if (radioButtonUi.width > _RadioMaxWidth) {
					_RadioMaxWidth = radioButtonUi.width;
				}
				
				if (radioButtonUi.height > _RadioMaxHeight) {
					_RadioMaxHeight = radioButtonUi.height;
				}
				
				if ( radioSettings["checked"] ) {
					_RadioButtonSelectedUi = radioButtonUi;
				}

				_RadioButtonsArray.push(radioButtonUi);

				addChild(radioButtonUi);
			}
			
			if ( _RadioButtonSelectedUi != null ) {
				$Select( _RadioButtonSelectedUi );
			}

			$Draw();
		}

		public function $Select( p_radioButtonUi:RadioButtonUi ) : void {
			_RadioButtonSelectedUi = p_radioButtonUi;
			
			var radioButtonUi : RadioButtonUi;
			for each ( radioButtonUi in _RadioButtonsArray ) {
				radioButtonUi.$IsChecked = false;
			}
			_RadioButtonSelectedUi.$IsChecked = true;
		}
		
		public function $SelectByName( p_txt:String ) : void {
			var radioButtonUi : RadioButtonUi;
			for each ( radioButtonUi in _RadioButtonsArray ) {
				if (radioButtonUi.$LabelUi.htmlText==p_txt){
					radioButtonUi.$IsChecked = true;
				}
			}
		}
		
		public function $Draw() : void {
			switch ( _Settings["orientation"] ) {
				case LayoutOrientation.$HORIZONTAL:
					_DrawHorizontal();
				break;
				case LayoutOrientation.$VERTICAL:
					_DrawVertical();
				break;
				case LayoutOrientation.$TABLE:
					_DrawTable();
				break;
			}
		}

		protected function _DrawTable() : void {
			var columns:Number = _Settings["columns"];
			
			if (columns <= 0) { columns = 2; }
			
			var nextX:Number = 0;
			var nextY:Number = 0;
			var radioButtonUi:RadioButtonUi;
			
			var i : uint;
			var arrayLength:uint = _RadioButtonsArray.length;
			var columnIndex : uint = 0;
			for (i = 0; i < arrayLength; i++ ) {
				while (columnIndex < columns && i < arrayLength) {
					radioButtonUi = _RadioButtonsArray[i] as RadioButtonUi;
					radioButtonUi.x = nextX;
					radioButtonUi.y = nextY;
					
					nextX += _RadioMaxWidth;
					columnIndex++;
					if (columnIndex < columns) {
						i++;
					}
				}
				columnIndex = 0;
				nextY += _RadioMaxHeight + _Settings["internalGap"];
				nextX = 0;
			}			
		}

		protected function _DrawVertical () : void {
			var nextX:Number = 0;
			var nextY:Number = 0;
			var radioButtonUi:RadioButtonUi;
			
			var i : uint;
			for (i = 0; i < _RadioButtonsArray.length; i++ ) {
				radioButtonUi = _RadioButtonsArray[i] as RadioButtonUi;
				radioButtonUi.x = nextX;
				radioButtonUi.y = nextY;
				if (_Settings["height"] == null) {
					nextY += _RadioMaxHeight;
				}else {
					nextX += _RadioMaxWidth;
					if ((nextY + _RadioMaxHeight) > _Settings["height"]) {
						nextY = 0;
						nextX += _RadioMaxWidth;
					}
				}
			}
		}

		protected function _DrawHorizontal () : void {
			var nextX:Number = 0;
			var nextY:Number = 0;
			var radioButtonUi:RadioButtonUi;
			
			var i : uint;
			for (i = 0; i < _RadioButtonsArray.length; i++ ) {
				radioButtonUi = _RadioButtonsArray[i] as RadioButtonUi;
				radioButtonUi.x = nextX;
				radioButtonUi.y = nextY;
				if (_Settings["width"] == null) {
					nextX += _RadioMaxWidth + _Settings["internalGap"];
				}
				else {
					nextX += _RadioMaxWidth;
					if ((nextX + _RadioMaxWidth) > _Settings["width"]) {
						nextX = 0;
						nextY += _RadioMaxHeight + _Settings["internalGap"];
					}
				}
			}
		}
	}

}